package com.rp.system.po;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

import org.codehaus.jackson.annotate.JsonIgnore;
import org.raptor.entity.BaseModel3;

/**
 * 权限
 * @author HuangJian
 *
 */
@Entity
@Table(name="RP_SYSTEM_AUTHORITIES")
public class Authority extends BaseModel3 {

	private static final long serialVersionUID = -9136622391401801514L;
	
	private String name;				//权限名称
	private String code;				//权限编码
	private String remark;				//备注
	
	private List<Role> roles;			//对应角色
	private List<Resource> resources = new ArrayList<Resource>();	//对应资源
	
	private String moduleId;			
	private String moduleName;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	
	public String getRemark() {
		return remark;
	}
	public void setRemark(String remark) {
		this.remark = remark;
	}
	
	@JsonIgnore
	@ManyToMany(mappedBy="authorities")
	public List<Role> getRoles() {
		return roles;
	}
	public void setRoles(List<Role> roles) {
		this.roles = roles;
	}
	
	@ManyToMany(fetch = FetchType.LAZY, 
			cascade = {CascadeType.PERSIST, CascadeType.REFRESH, CascadeType.MERGE})
	@JoinTable(
			name = "RP_SYSTEM_AUTHORITY_RESOURCE", 
			joinColumns = { @JoinColumn(name = "authority_id") }, 
			inverseJoinColumns = { @JoinColumn(name = "resource_id") })
	public List<Resource> getResources() {
		return resources;
	}
	public void setResources(List<Resource> resources) {
		this.resources = resources;
	}
	
	public String getModuleId() {
		return moduleId;
	}
	public void setModuleId(String moduleId) {
		this.moduleId = moduleId;
	}
	public String getModuleName() {
		return moduleName;
	}
	public void setModuleName(String moduleName) {
		this.moduleName = moduleName;
	}
	
	/**
	 * 重写equals方法，如果ID相同，则判断的两个权限相同
	 */
	@Override
	public boolean equals(Object obj) {
		if (obj instanceof Authority) {
			Authority auth = (Authority) obj;
			if (auth.getId().equals(this.getId())) {
				return true;
			}
		}
		return false;
	}
	
}
